﻿using System;
using System.Xml.Xsl;
using Demo.CommonBase.View.Forms;
using Demo.Services.Contracts.DTO.Filters;
using Ext.Net;
using Demo.CommonBase.DTO.Infos;
using Demo.Services.Contracts.DTO.Infos;
using Demo.CommonBase.Logging;
using Demo.Services.Services.Impl.Managers;
using Demo.CommonBase.Exceptions;
using System.Reflection;

namespace Demo.Web.Catalogs
{
    public partial class Area : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private AreaFilter GetFilter()
        {
            var filter = new AreaFilter {Name = txtDescription.Text};
            return filter;
        }
        protected void DsAreaReadData(object sender, StoreReadDataEventArgs e)
        {
            AreaFilter filter;
            AreaManager areaManager;
            PaginationInfo areaPaging;
            AreaResultInfo result;
            try
            {
                Logger.Info();
                filter = GetFilter();
                areaPaging = new PaginationInfo {PageNum = e.Start, PageSize = e.Limit};

                DataSorter sort = e.Sort[0];
                areaPaging.Sort = sort.Property;
                areaPaging.Direction = sort.Direction.ToString(); 
                areaManager = new AreaManager();
                result = areaManager.GetAreas(areaPaging, filter);
                e.Total = result.TotalReg;
                gpArea.GetStore().DataSource = result.AreaList;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
        }
        protected void BtnSearchClick(object sender, DirectEventArgs e)
        {
            dsArea.RemoveAll();
            dsArea.Reload();
        }       
        protected void BtnNewClick(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();
                AreaWindow.Title = "Nueva Area";
                AreaWindow.Show();
                var loader = new ComponentLoader {AutoLoad = true, Url = "AreaEdit.aspx"};
                AreaWindow.LoadContent(loader);

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }

        }
        protected void BtnDeleteConfirmClick(object sender, DirectEventArgs e)
        {
            int id = 0;
            if (e.ExtraParams["id"] != null)
            {
                id = Convert.ToInt32(e.ExtraParams["id"]);
            }

            ExtNet.Msg.Confirm("Confirmacion", "Desea Borrar Registro?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = string.Format("Demo.DoYes({0})", id),
                    Text = "Si"
                },
                No = new MessageBoxButtonConfig
                {
                    Handler = "Demo.DoNothing()",
                    Text = "No",
                }
            }).Show();
        }
        [DirectMethod]
        public void DoYes(int id)
        {
            AreaManager areaManager;
            areaManager = new AreaManager();
            areaManager.DeleteArea(id);
            Notification.Show(new NotificationConfig
            {
                Title = "Información",
                Icon = Icon.Information,
                Html = "El registro fué borrado",
                HideDelay = 3000
            });


            dsArea.RemoveAll();
            dsArea.Reload();
        }

        [DirectMethod]
        public void DoNothing()
        {
 
        }
        [DirectMethod]
        public void SaveAndRefresh(object sender, DirectEventArgs e)
        {
            Notification.Show(new NotificationConfig
            {
                Title = "Información",
                Icon = Icon.Accept,
                Html = "El registro fué Guardado Exitosamente",
                HideDelay = 3000
            });
            dsArea.Reload();
        }
        protected void ToExcel(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();
            var eSubmit = new StoreSubmitDataEventArgs(json, null);
            var xml = eSubmit.Xml;

            Response.Clear();
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment; filename=Tecnologia.xls");
            var xtExcel = new XslCompiledTransform();
            xtExcel.Load(Server.MapPath("..\\ExportConfig\\Excel.xsl"));
            xtExcel.Transform(xml, null, Response.OutputStream);
            Response.End();
        }
              
    }
}